// pages/functionPage/qrcode_change/qrcode_change.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:'',
    content:'',
    result:''
  },

  // 保存二维码图片
addImage(){
  if(this.data.content == ''){
    wx.showToast({
      title: '请输入任务备注',
      icon:'none'
    })
    return;
  }
  
  wx.showLoading({
    title: '生成中',
  })
  let that = this
  let url = `https://mygj.xyz/get_qrcode?content=${this.data.content}`
  wx.getSetting({
      success: (res) => {
          if(res.authSetting['scope.writePhotosAlbum']){
              that.saveImg(url)
          }else if(res.authSetting['scope.writePhotosAlbum'] === undefined){
              wx.authorize({
                  scope: 'scope.writePhotosAlbum',
                  success() {
                      that.saveImg(url)
                  },
                  fail(){
                       wx.showToast({
                          title: '您没有授权，无法保存到相册',
                          icon: 'none'
                      })
                  }
              })
          }else{
              wx.openSetting({
                  success(res) {
                      if (res.authSetting['scope.writePhotosAlbum']) {
                          that.saveImg(url)
                      }else{
                          wx.showToast({
                              title:'您没有授权，无法保存到相册',
                              icon:'none'
                          })
                      }
                  }
              })
          }
      }
  })
},

// 保存图片
saveImg(url){
  wx.downloadFile({  //下载图片到本地
    url: url,
    success (res) {
      if (res.statusCode === 200) {
          let img = res.tempFilePath
          wx.saveImageToPhotosAlbum({  //只支持本地图片所以要先把图片下载下来
              filePath: img,
              success(result) {
                 wx.showToast({
                     title:'已保存至相册',
                     icon:"none",
                 })
              },fail(res) {
                  wx.showToast({
                     title:'保存失败',
                     icon:"none",
                  })
              }
          })
      }
    }
  })
},

  // 处理输入框输入
  url_input(e){
    this.setData({
      content:e.detail.value
    })
  },

  // 上传图片识别
  upload_img(){
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(res)
        that.setData({
          src:tempFilePaths
        })
      }
    })
  },

  // 扫描
  scan(){
    var that = this
    wx.scanCode({
      success (res) {
        that.setData({
          result:res.result
        })
        wx.showToast({
          title: '识别成功',
          success:function(){
            that.open_url()
          }
        })

      }
    })
  },

  // 打开url
  open_url(){
    var that = this
    if(this.data.result == ''){
      wx.showToast({
        title: '暂无识别出的url',
        icon:'none'
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '二维码识别内容：'+this.data.result,
      success (res) {
        if (res.confirm) {
          that.setData({
            content:that.data.result
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 复制识别内容
  copy_content(){
    if(this.data.result == ''){
      wx.showToast({
        title: '暂无识别内容',
        icon:'none'
      })
      return;
    }
    wx.setClipboardData({
      data: this.data.result,
      success:function(){
      }
    })
  },

  // 清空输入框
  clear(){
    this.setData({
      content:''
    })
  },

  // 获取复制内容
  get_text(){
    var that = this
     wx.getClipboardData({
       success: (option) => {
         console.log(option)
         if(option.data == ''){
          return;
        }
         wx.showModal({
          title: '提示',
          content: '系统检测到您复制的内容，是否粘贴到您的输入框',
          success (res) {
            if (res.confirm) {
              that.setData({
                content:option.data
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
       },
     })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.get_text()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})